package com.zhentao.controller;

import com.zhentao.pojo.Viporder;
import com.zhentao.service.UserService;
import com.zhentao.service.ViporderService;
import com.zhentao.service.ViptaocanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author gyz
 * @since 2025-09-27
 */
@RestController
@RequestMapping("/viporder")
public class VipOrderController {
    @Autowired
    ViptaocanService viptaocanService;
    @Autowired
    ViporderService viporderService;
    @Autowired
    UserService userService;
    //获取所有vip套餐
    @RequestMapping("viptaocanAll")
    public Object getViptaocanAll(){
        return viptaocanService.list();
    }
    //创建新的vip订单
    @RequestMapping("vipOrderAdd")
    public Object getvipOrderAdd(Integer userid,Integer viptcid){
        Viporder viporder=new Viporder();
        viporder.setUserid(userid);
        viporder.setViptcid(viptcid);
        viporderService.save(viporder);
        return "添加成功";
    }
    //取消指定订单
    @RequestMapping("delOrder")
    public Object delOrder(Integer id){
        try {
            // 参数验证
            if (id == null) {
                return "订单ID不能为空";
            }
            
            // 检查订单是否存在
            Viporder order = viporderService.getById(id);
            if (order == null) {
                return "订单不存在";
            }
            
            // 检查订单状态，只有未支付的订单才能取消
            if (order.getStatus() != null && order.getStatus() == 1) {
                return "已支付订单无法取消";
            }
            
            // 删除订单
            boolean success = viporderService.removeById(id);
            if (success) {
                return "取消订单成功";
            } else {
                return "取消订单失败";
            }
            
        } catch (Exception e) {
            System.err.println("取消订单异常: " + e.getMessage());
            return "系统错误，取消失败";
        }
    }
    //获取指定用户的vip订单
    @RequestMapping("viptaocanById")
    public Object getviptaocanById(Integer userid){
        List<Viporder> list = viporderService.list();
        List<Viporder> list1=new ArrayList<>();
        for (Viporder viporder : list) {
            if(viporder.getUserid().equals(userid)){
                viporder.setUser(userService.getById(viporder.getUserid()));
                viporder.setViptaocan(viptaocanService.getById(viporder.getViptcid()));
                list1.add(viporder);
            }
        }
        return  list1;
    }

}
